<template>
    <div class="center-map">
        <div class="china-map-box">
            <div class="form-container">
                <slot name="rightForm"></slot>
            </div>
            <div style="flex: 1; overflow: hidden; margin: 0 0 10px; height: 100%">
                <div id="mapSecondMap" class="mapSecondMap"></div>
            </div>
            <div v-show="false" id="container"></div>
            <div v-show="false" id="panel"></div>
            <div v-show="isShow" :class="className" class="footer-data" style="--moveUp: 20px; --moveDown: -80px">
                <div
                    v-for="item in footerList"
                    :style="{ cursor: item.key == 'allotDeviceNum' ? 'pointer' : 'default' }"
                    class="col-item nowrap"
                    @click="openWatchList(item)"
                >
                    <div class="col-text">{{ item?.name }}</div>
                    <div :title="item.value" class="col-value nowrap">{{ item?.value }}</div>
                </div>
            </div>
            <div
                v-show="isShowAlarm"
                :class="isShowAlarm ? 'slide-top' : 'slide-bottom'"
                class="footer-dialog-con"
                style="--moveUp: 20px; --moveDown: -180px"
            >
                <FooterDialog :detailLeft="detailLeft" :detailRight="detailRight" @closeDialog="closeDialog"></FooterDialog>
            </div>
        </div>
        <WatchListDialog ref="watchDialogRef" @close="closeWatch"></WatchListDialog>
    </div>
</template>
<script lang="ts" setup>
    import { onMounted, ref, watch } from 'vue';
    import { Map } from '/@/assets/mapUtils/map';
    import $bus from '/@/utils/bus.ts';
    import FooterDialog from '/@/components/secondaryScreen/secondMap/components/footerDialog.vue';
    import WatchListDialog from '/@/components/secondaryScreen/secondMap/components/watchListDialog.vue';
    import { infoContent, useAlarmDetail, useFooterTable, useMoveClass } from '/@/components/secondaryScreen/secondMap/secondMapHooks.ts';
    import { footerApi, mapListApi } from '/@/components/secondaryScreen/secondMap/secondMapApi.ts';
    import { earlyWarning } from '/@/utils/busConstant.ts';

    const props = defineProps({
        orgCode: String,
        type: String,
    });
    const mapList = ref([]); //地图数据
    // 底部表格
    const { isShow, className, setIsShow } = useMoveClass();

    function init() {
        let mapContainer = document.querySelector('.mapSecondMap');
        mapContainer &&
            Map.initMap({ el: 'mapSecondMap' }).then(() => {
                getMapList();
            });
    }

    const watchDialogRef = ref();

    function openWatchList(item) {
        if (item.key == 'allotDeviceNum') {
            watchDialogRef.value.openModal(item.extData?.deptId);
        }
    }

    const { footerList, setFooterList, setIsShowAlarm, isShowAlarm } = useFooterTable();

    async function getFooterTable(orgCode) {
        let params = {
            orgCode: orgCode || props.orgCode,
        };
        const { code, result } = await footerApi(params);
        if (code == 200) {
            setIsShow(true);
            setFooterList(result);
        }
    }

    async function getMapList() {
        let orgCode = props.orgCode?.substring(0, 6);
        getFooterTable(orgCode);
        try {
            const { code, result } = await mapListApi({ orgCode: props.orgCode });
            if (code == 200 && result.length > 0) {
                const newArr = result.map((item) => ({ ...item, type: '6' }));
                mapList.value = newArr;
                Map.createOverlay(
                    newArr,
                    {
                        content: (val) => {
                            return `<div style="
                                width: 21px;
                                height: 44px;
                                position: absolute;
                                left: -20px;
                                top: -20px;" title="${val?.departName}">
                            </div>`;
                        },
                    },
                    async (val) => {
                        let params = {
                            orgCodeLeaf: val.orgCode,
                            orgCode: null,
                        };
                        const { code, result } = await footerApi(params);
                        if (code == 200) {
                            Map.createInfoModal({ option: val, content: infoContent(result) });
                            setIsShow(true);
                        }
                    }
                );
            } else {
                Map.removeOverlayGroup();
            }
        } catch {}
    }

    watch(
        () => props.orgCode,
        () => {
            getMapList();
        }
    );
    const { detailLeft, detailRight, setDetail } = useAlarmDetail();
    onMounted(() => {
        init();
        //点击预警定位
        $bus.on(earlyWarning, (params) => {
            setDetail(params);
            drawMarker(params);
            setIsShowAlarm(true);
        });
    });

    /**
     *@desc 底部信息
     */
    function closeDialog() {
        setIsShowAlarm(!isShowAlarm.value);
        Map.clearMarker();
    }

    /**
     * @desc  预警弹窗关闭
     */
    function closeWatch() {}

    function drawMarker(data: any) {
        const { lon, lat } = data;
        if (!lon || !lat) {
            Map.clearMarker();
            return;
        }
        Map.createMarker({ ...data, realName: data.userName });
    }
</script>
<style lang="less" scoped>
    .center-map {
        flex: 1;
        color: #fff;
        display: flex;
        flex-direction: column;
        align-content: space-between;

        .china-map-box {
            flex: 1;
            display: flex;
            flex-direction: column;
            position: relative;
            overflow: hidden;
        }

        .form-container {
            position: absolute;
            right: 0;
            top: 17px;
            z-index: 1;
        }

        .mapSecondMap {
            height: calc(100% + 20px);
            margin: 10px 0;
        }

        .footer-data {
            position: absolute;
            left: 4px;
            right: 4px;
            bottom: var(--moveDown);
            display: flex;
            z-index: 999;
            border: 1px solid rgba(35, 132, 221);

            .col-item {
                flex: 1;
                color: #ffffff;
                text-align: center;

                .col-text {
                    height: 40px;
                    line-height: 40px;
                    background: rgba(35, 132, 221, 0.5);
                }

                .col-value {
                    height: 40px;
                    line-height: 40px;
                    background-color: #00152b;
                }
            }
        }

        .footer-dialog-con {
            position: absolute;
            left: 4px;
            right: 4px;
            bottom: var(--moveDown);
            z-index: 999;
        }

        .slide-top {
            animation: slide-top 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94) both;
        }

        @keyframes slide-top {
            0% {
                //transform: translateY(var(--moveUp));
                bottom: var(--moveDown);
            }
            100% {
                //transform: translateY(var(--moveDown));
                bottom: var(--moveUp);
            }
        }

        .slide-bottom {
            animation: slide-bottom 0.5s cubic-bezier(0.25, 0.46, 0.45, 0.94) both;
        }

        @keyframes slide-bottom {
            0% {
                //transform: translateY(var(--moveDown));
                bottom: var(--moveUp);
            }
            100% {
                //transform: translateY(var(--moveUp));
                bottom: var(--moveDown);
            }
        }
    }

    :deep(.watch-modal) {
        width: 300px;
        height: 210px;
        display: flex;
        flex-direction: column;
        justify-content: center;
        padding-left: 10px;

        .modal-row {
            font-size: 16px;
            line-height: 34px;
            color: #fff;

            .label {
                display: inline-flex;
                justify-content: flex-end;
                width: 140px;
            }
        }
    }
</style>
